package com.admin.web;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.admin.service.AdminSchoolService;
import com.common.model.AdminUser;
import com.common.model.BaseUser;
import com.common.model.Department;
import com.common.model.ProfBranch;
import com.common.model.Profession;

@Controller
@RequestMapping("/admin/department")
public class AdminDepartmentWeb {
	@Autowired
	AdminSchoolService service;
	/**
	 * 根据条件查询系
	 * @param clgId 学院id
	 * @param page 页码(从1开始)
	 * @return
	 */
	@RequestMapping("/showDepartmentList/{clg_id}/{page}")
	public ModelAndView showDepartment(
			@PathVariable("clg_id") int clgId,
			@PathVariable("page")int page){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("admin/school/showDepartment");
		mav.addObject("colleges", service.getAllCollege());
		mav.addObject("clgId", clgId);
		int departmentCount=service.getDepartmentCountBy(clgId);
		int totalPage=(int)Math.ceil(departmentCount/10.0);
		if(totalPage==0){
			totalPage=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		mav.addObject("page", page);
		mav.addObject("department",service.getDepartmentBy(clgId, page));
		mav.addObject("departmentCount",departmentCount );
		mav.addObject("totalPage",totalPage);
		mav.addObject("currentMenu", "showDepartmentList");
		return mav;
	}
	/**
	 * 根据系id获取系
	 * @param depId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getDepartment/{dep_id}")
	public Department getDepartmentByDepId(@PathVariable("dep_id")int depId){
		return service.getDepartmentByDepId(depId);
	}
	@ResponseBody
	@RequestMapping("/updateDepartment")
	public int updateDepartment(Department dep,HttpSession session){
		if(dep.getId()==null||dep.getCollegeId()==null||dep.getName()==null||dep.getName().trim().isEmpty()||dep.getNo()==null||dep.getNo().trim().isEmpty()){
			return -3;
		}
		AdminUser admin=(AdminUser) session.getAttribute("user");
		//检查正在操作的用户是否为超级管理员
		if(!admin.isSuperAdmin()){
			//如果不是超级管理员,检查正在操作的系是否为本学院的
			int orgCollegeId=service.getDepartmentByDepId(dep.getId()).getCollege().getId();
			if(admin.getCollegeId()!=orgCollegeId){
				return -1;//操作之前不是本学院的
			}
			int newCollegeId=dep.getCollegeId();
			if(admin.getCollegeId()!=newCollegeId){
				return -2;//操作之后不是本学院的
			}
		}
		Department depCheck=service.getDepartmentByDepNo(dep.getNo());
		if(depCheck!=null&&depCheck.getId()!=dep.getId()){
			return -4;//重复no;
		}
		Department depNameCheck=service.getDepartmentByDepName(dep.getName());
		if(depNameCheck!=null&&depNameCheck.getId()!=dep.getId()){
			return -5;//重复name;
		}
		dep.setName(dep.getName().trim());
		dep.setNo(dep.getNo().trim());
		try {
			service.updateDepartment(dep);
		} catch (Exception e) {
			return -6;//参数错误2
		}
		return 0;
	}
	/**
	 * 删除一个系
	 * @param depId 
	 * @param session
	 * @return -1 无权限 -2 有教师没有删除 -3 有专业没有删除 0 成功
	 */
	@ResponseBody
	@RequestMapping("/deleteDepartment/{dep_id}")
	public int deleteDepartment(@PathVariable("dep_id")int depId,HttpSession session){
		AdminUser admin=(AdminUser) session.getAttribute("user");
		//检查正在操作的用户是否为超级管理员
		if(!admin.isSuperAdmin()){
			//如果不是超级管理员,检查正在操作的系是否为本学院的
			int orgCollegeId=service.getDepartmentByDepId(depId).getCollege().getId();
			if(admin.getCollegeId()!=orgCollegeId){
				return -1;//操作之前不是本学院的
			}
		}
		int result=service.deleteDepartment(depId);
		if(result==0){
			return 0;
		}
		return result-1;
	}
	/**
	 * 新建一个系
	 * @param dep
	 * @return -1 权限不足  -2 参数错误 -3 有相同no的系存在  -4 有相同name的系存在-5 参数错误 0 成功
	 */
	@ResponseBody
	@RequestMapping("/insertDepartment")
	public int insertDepartment(Department dep,HttpSession session){
		if(dep.getName()==null||dep.getName().trim().isEmpty()||dep.getNo()==null||dep.getNo().trim().isEmpty()||dep.getCollegeId()==null){
			return -2;//参数错误
		}
		AdminUser admin=(AdminUser) session.getAttribute("user");
		//检查正在操作的用户是否为超级管理员
		if(!admin.isSuperAdmin()){
			//如果不是超级管理员,检查添加的系是否为本学院的
			int newCollegeId=dep.getCollegeId();
			if(admin.getCollegeId()!=newCollegeId){
				return -1;//添加的系不属于本学院
			}
		}
		dep.setName(dep.getName().trim());
		dep.setNo(dep.getNo().trim());
		int result=service.insertDepartment(dep);
		if(result==-1){
			return -3;//已存在同样no的系
		}
		if(result==-2){
			return -4;//参数错误2
		}
		if(result==-2){
			return -5;//参数错误2
		}
		return 0;
	}
}
